

<?php

class AppController extends Zend_Controller_Action {

    public function indexAction() {
        $this->buildHead();
        $model = new Model_App();
        $rs = $model->getAllApp();
        $this->view->apps = $rs;
    }

    public function deleteAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $id = $this->_request->getParam("id");
        if ($id != null) {
            $model = new Model_App();
            $result = $model->deleteApp($id);
            if ($result != null) {
                echo $this->returnDone();
            } else {
                echo $this->returnFail();
            }
        }
    }

    public function addAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $appid = $this->_request->getParam("appId");
        $apik = $this->_request->getParam("apik");
        $secretk = $this->_request->getParam("secretk");
        $holder = $this->_request->getParam("holder");
        if ($appid != null && $apik != null && $secretk != null && $holder != null)  {
            $model = new Model_App();
            $result = $model->insertApp($appid, $apik, $secretk, $holder);
            if ($result != null) {
                echo $this->returnDone();
            } else {
                echo $this->returnFail();
            }            
        }        
    }

    private function buildHead() {
        $base = $this->_request->getBaseUrl();
        $this->view->base = $base;
        $this->view->delete = $base . '/app/delete';
        $this->view->baseAdd = $base . "/app/add";
        $this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $this->view->headScript()->appendFile($base . "/js/Ajax.js", 'text/javascript');
        $this->view->headScript()->appendFile($base . "/js/sunnyAjax.js", 'text/javascript');
    }

    private function returnDone() {
        $array = array(
            "errorCode" => 0,
            "errorMessage" => "Query completed.",
        );
        echo json_encode($array);
    }

    private function returnFail() {
        $array = array(
            "errorCode" => -1,
            "errorMessage" => "Query fail.",
        );
        echo json_encode($array);
    }

}
?>

